<template lang="pug">
div
  CascadeFilter(
      :showFilter="showFilter"
      :filterData="filterData"
      :cascade="cascade"
      @close="showFilter = false"
      @confirm="confirm"
      )

  FixedScroll
    div(slot="fixed")
      PageHeader(pageTitle="器具库存查询")
        div(slot="right" @click="showFilter = true") 筛选

    StatTable.table(
        slot="scroll"
        :headList="headList"
        :body="body"
        :isLoading="isLoading"
        )

    Pager(
        slot="fixed-bottom"
        :maxIndex="maxIndex"
        :curIndex="curIndex"
        @jump="jump"
        )

</template>

<script>
import api from "$api"
import util from "$util"

const PLANT = "2290"

export default {
  data() {
    // 设置级联
    let cascade = [
      ["StorgelocationCode"],
    ]

    // 配置筛选
    let filterData = [
      {
        k: "StorgelocationCode",
        kTitle: "实体仓库",
        tag: "dropdown",
        async getOptionList() {
          try {
            let data = await api.StockFilterGetReal({
              Plant: PLANT,
            })
            let newData = []
            for (let item of data) {
              if (item.ItemValue !== "") {
                newData.push({
                  id: item.ItemValue,
                  name: item.ItemText,
                })
              }
            }
            return newData
          } catch(err) {
            throw err
          }
        },
      },
      {
        k: "SupplierNum",
        kTitle: "厂家编号",
        tag: "input",
      },
      {
        k: "PackageNo",
        kTitle: "器具编号",
        tag: "input",
      },
    ]

    return {
      searchValue: "",
      isLoading: false,
      showFilter: false,
      filterData,
      cascade,

      loading: {
        confirm: false,
      },

      curIndex: 1,
      maxIndex: 1,
      lastReq: { // 记住上一次请求是搜索还是筛选，跳转页面时要用
        type: "",
        param: null,
      },

      headList: [
        { field: "PackageNo", title: "器具编号", checked: true },
        { field: "StorgelocationCode", title: "实体仓库", checked: true },
        { field: "WmNo", title: "逻辑仓库", checked: true },
        { field: "SupplierNum", title: "厂家编号", checked: true },
        { field: "SupplierName", title: "厂家简称", checked: true },
        { field: "Stock", title: "数量", checked: true },
      ],
      body: {
        list: [],
      },
    }
  },
  methods: {
    // 筛选确定
    async confirm(filter, pageIndex = 1, pageSize = 20) {
      if (this.loading.confirm) {
        return
      }

      let param = { Plant: PLANT }
      for (let item of filter) {
        param[item.key] = item.value
      }

      param.startRowIndex = (pageIndex - 1) * pageSize + 1
      param.maximumRows = pageSize
      this.lastReq.type = "confirm"
      this.lastReq.param = filter

      this.isLoading = true
      this.loading.confirm = true
      try {
        let data = await api.TrayStockFilter(param)
        if (!data || !data.NewPackageStocksList || data.NewPackageStocksList.length === 0) {
          util.Toast("没有找到符合条件的记录")
          return
        }
        this.body.list = data.NewPackageStocksList
        this.maxIndex = Math.ceil(data.total / pageSize)
        this.curIndex = pageIndex
      } catch(err) {
        this.$root.error(err)
      } finally {
        this.isLoading = false
        this.loading.confirm = false
      }
    },

    jump(p) {
      if (this.lastReq.type === "confirm") {
        this.confirm(this.lastReq.param, p.curIndex)
      }
    },

  },

}
</script>

<style lang="stylus" scoped>

.table
  scan-table()

</style>
